//
//  Action1ViewController.swift
//  testRxSwift
//
//  Created by 火星人 on 2017/7/27.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift
import Action

class Action1ViewController: BaseViewController {

    var disposed = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let tf = UITextField(frame: CGRect(x: 0, y: 80, width: self.view.frame.width, height: 33))
        tf.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2)
        
        self.view.addSubview(tf)
        
        
        let label = UILabel(frame: CGRect(x: 0, y: 130, width: self.view.width, height: 33))
        self.view.addSubview(label)
        
        
        let action: Action<String, String> = Action{
            (username) in
            print("\(username)")
            if username.length > 4 {
                return Observable.just("验证通过")
            }
            else{
                return Observable.just("验证失败")
            }
        }
        
        tf.rx.text.asObservable()
            .map({ (value) -> String in
                return value ?? ""
            })
            .bind(to: action.inputs)
            .disposed(by: disposed)
        
        action.elements
            .bind(to: label.rx.text.asObserver())
            .disposed(by: disposed)
        
    }
}
